'''
Author: gaoh
Date: 2023-07-21 22:55:22
LastEditTime: 2023-07-24 23:51:37
'''
import requests
import re

# -------------------------------- 1 --------------------------------

# resp = requests.get('https://www.baidu.com')
# print(type(resp))
# print(resp.status_code)
# print(type(resp.text))
# print(resp.cookies)
# print(resp.text)


# -------------------------------- get-data --------------------------------
# resp=requests.get('http://httpbin.org/get')
# print(resp.text)


# -------------------------------- params --------------------------------

# data={
#     'name':'gaoh',
#     'age':'25'
# }
# resp=requests.get('http://httpbin.org/get',params=data)
# print(resp.text)

# print(resp.json())

# -------------------------------- 正则获取内容 --------------------------------

# headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
#            }

# resp = requests.get("https://www.zhihu.com/explore", headers=headers)
# pattern = re.compile('explore-feed.*?question_link.*?>(.*?)</a>', re.S)
# titles = re.findall(pattern, resp.text)
# print(titles)


# -------------------------------- 获取文件 --------------------------------

# resp=requests.get("https://github.com/favicon.ico")
# print(resp.text)
# print(resp.content)


# -------------------------------- 爬取文件 --------------------------------

# resp=requests.get("https://github.com/favicon.ico")


# with open('favicon.ico','wb') as f:
#     f.write(resp.content)


# -------------------------------- POST --------------------------------

# data={
#     'name':'gaoh',
#     'age':'25'
# }

# resp=requests.post('http://httpbin.org/post',data=data)
# print(resp.text)


# -------------------------------- 响应 --------------------------------

# r = requests.get('http://www.jianshu.com')
# print(type(r.status_code), r.status_code)
# print(type(r.headers), r.headers)
# print(type(r.cookies), r.cookies)
# print(type(r.url), r.url)
# print(type(r.history), r.history)


# -------------------------------- 文件上传 --------------------------------

# files = {'file': open('favicon.ico', 'rb')}
# resp=requests.post('http://httpbin.org/post',files=files)
# print(resp.text)


# -------------------------------- coockies --------------------------------


# resp=requests.get('https://www.baidu.com')
# print(resp.cookies)
# for key,val in resp.cookies.items():
#     print(key+'='+val)


# headers = {
#     'Cookie': 'BIDUPSID=C4D0DDDA42027884F2FD6AD40CE3B7D8; PSTM=1619001596; BDUSS=lB2S0hIWUtpejVQbzBweTBaUTVNajc1ZlhpeS05QzcwNmMzVFJPbzBFZlY0dHRoRVFBQUFBJCQAAAAAABAAAAEAAACUFw~7Z2FvaHdhbmdoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANVVtGHVVbRhT; BDUSS_BFESS=lB2S0hIWUtpejVQbzBweTBaUTVNajc1ZlhpeS05QzcwNmMzVFJPbzBFZlY0dHRoRVFBQUFBJCQAAAAAABAAAAEAAACUFw~7Z2FvaHdhbmdoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANVVtGHVVbRhT; BAIDUID=C09F4B7E08755FF5EE54D03589965CEE:FG=1; ZFY=IQ6zMyD6HanEJC76gQJzIyu3hWs24l:Ahe3:BGzVpjERI:C; BAIDUID_BFESS=C09F4B7E08755FF5EE54D03589965CEE:FG=1; baikeVisitId=70916fc4-33d3-47fd-8d8f-223555a3dd86; BD_HOME=1; H_PS_PSSID=36545_38643_38831_38942_38881_39114_39119_39037_38917_38827_39089_26350_39101_39044; BD_UPN=12314753; BA_HECTOR=8h012h0h05ak25250kah2k8l1ibt4h61o',
#     'Host': 'www.zhihu.com',
#     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36',
# }
# resp=requests.get('https://www.zhihu.com',headers=headers)
# print(resp.text)


# cookies='d_c0="ALDeLRwDAhOPTiq-VZ1gPXwosyFSEc-XbTM=|1619332547"; _9755xjdesxxd_=32; YD00517437729195%3AWM_TID=FDNT3dJpwdJBARERVBd%2B86mURZZGCmgy; _xsrf=m45Qi8GXmtKISjAb6ZEs2LJlkwp0Xz6K; YD00517437729195%3AWM_NI=dXULMT08BH0%2BqnXEvPVrg38sFIaNnIS4hyJj2nQf5Mb5WWY2Oe4uY1PgXvQO7oENw5%2BdQQLaWSUFZcx7PcfooglVLvsJPLBJvTOs8%2F2V4W%2BRm4X8JOIauHWrTkxFfql3UDQ%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda170e2e6eed2e967b6bb88b4fc64829e8aa6d15f978f9a82d5438b9a99a6f06ef18be58feb2af0fea7c3b92aa8889badd060f4e88ea9b321b3b298a6f55c96aea9d4bb798697afafed4391eca89ac962f2b29f87b847a2eb99b8dc3a8dbbfc96dc3b94b99897d445f5b18baff63fe9bc9fb2cf6b958d9ba7d969bce9a084e565adb9a4b7cf7eb196a0adc142b68c8194bb25f28afcb8aa5aafec98d9c9458bbfbba9d67a98b5b8a7e57bb0ed9ba5d437e2a3; _zap=1a0efa62-0421-47b2-bb78-8f2e652b4ecc; z_c0=2|1:0|10:1689261231|4:z_c0|80:MS4xWEE2a0lRQUFBQUFtQUFBQVlBSlZUUmxhbldYRFg3WnVXQmIyRmY5ZmJFU3BUWjV6RTFtNzBRPT0=|6ff2c991b07faa12ebcc713bff1925875d2ef9187758d348edb3ba301eb9fcb9; q_c1=ca8bcb6268f640b2b5d2c878ce41712f|1690211432000|1690211432000; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1689261389,1689869443,1689952097,1690211605; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1690211605; tst=r; SESSIONID=GQ1bMe1U7qKTmSxJKQmEPHNbr0d7l9G4tk4LsDipMHA; JOID=VVsWA0oMWjxg_RSXcwu4pXLgEJdkTwsOA7Z1piU9L3c3kUHrOWbNgAP_EJRx-BSCMeoMa0BFL2zb9DYP4xzHby4=; osd=VFAdBU4NUTdm-RWceA28pHnrFpNlRAAIB7d-rSM5Lnw8l0XqMm3LhAL0G5J1-R-JN-4NYEtDK23Q_zAL4hfMaSo=; KLBRSID=031b5396d5ab406499e2ac6fe1bb1a43|1690211661|1690211432'
# jar = requests.cookies.RequestsCookieJar()
# headers = {
#     'Host': 'www.zhihu.com',
#     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36'
# }

# for cookie in cookies.split(';'):
#     key,val=cookie.split('=',1)
#     jar.set(key,val)
# resp = requests.get('https://www.zhihu.com',cookies=jar,headers=headers)
# print(resp.text)
 



# -------------------------------- Session  --------------------------------

# requests.get('http://httpbin.org/cookies/set/number/123456789')
# r = requests.get('http://httpbin.org/cookies')
# print(r.text)

# 通过session保持一个cookies
# s = requests.Session()
# s.get('http://httpbin.org/cookies/set/number/123456789')
# r = s.get('http://httpbin.org/cookies')
# print(r.text)

# -------------------------------- SSL  --------------------------------

# from requests.packages import urllib3

# urllib3.disable_warnings()
# response = requests.get('https://www.12306.cn', verify=False)
# print(response.status_code)

# import logging

# # 忽略日志警告
# logging.captureWarnings(True)
# response = requests.get('https://www.12306.cn', verify=False)
# print(response.status_code)


# -------------------------------- 代理设置  --------------------------------



# proxies = {
#   'http': 'http://127.0.0.1:7890',
#   'https': 'http://127.0.0.1:7890',
# }

# resp=requests.get('https://www.taobao.com', proxies=proxies)
# print(resp.status_code)

# -------------------------------- 超时  --------------------------------


# # 设置连接 和 响应结果得超时时间
# resp=requests.get('https://www.taobao.com',timeout=(5,10))

# # 不设置超时默认就是
# resp = requests.get('https://www.taobao.com', timeout=None)


# -------------------------------- prepare请求数据  --------------------------------

from requests import Request,Session

url='http://httpbin.org/post'
data={'name':'gaoh'}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36'
}
s=Session()
req=Request('post',url=url,data=data,headers=headers)
prepare=s.prepare_request(req)
resp=s.send(prepare)
print(resp.text)
